﻿namespace PropOnline.Stores.Data
{
    using System;
    using System.Data;
    using System.Dynamic;

    public class DynamicDataReader : DynamicObject
    {
        private IDataRecord obj;

        public DynamicDataReader(IDataRecord obj)
        {
            this.obj = obj;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            try
            {
                if (!this.obj.IsDBNull(this.obj.GetOrdinal(binder.Name)))
                {
                    result = this.obj.GetValue(this.obj.GetOrdinal(binder.Name));
                }
                else
                {
                    result = null;
                }

                return true;
            }
            catch (Exception)
            {
                result = null;
                return false;
            }
        }
    }
}